package com.zlsy.secondtime.threadcoreknowledge.threadobjectclasscommonmethods;

/**
 * @author zhouliang
 * @date 2020/5/18 8:28
 * <p>
 * 展示线程sleep的时候不会释放synchronized的monitor，等sleep的时间到了以后
 * 正常的结束才释放锁。
 */
public class SleepDontReleaseMonitor implements Runnable {

    @Override
    public void run() {
        sync();
    }

    private synchronized void sync() {
        try {
            System.out.println(Thread.currentThread().getName() + "进入休眠");
            Thread.sleep(3000);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
        System.out.println(Thread.currentThread().getName() + "执行完毕");
    }

    public static void main(String[] args) {
        SleepDontReleaseMonitor r = new SleepDontReleaseMonitor();
        Thread thread = new Thread(r);
        thread.setName("线程1");
        Thread thread1 = new Thread(r);
        thread1.setName("线程2");

        thread.start();
        thread1.start();
    }
}
